package com.dooioo.pns.business.service;

import com.dooioo.pns.base.model.Pagination;
import com.dooioo.pns.base.service.BaseService;
import com.dooioo.pns.entity.model.DeviceInfo;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * PackageName:com.dooioo.support.service
 * Author: Jerry.hu
 * Create: Jerry.hu (2015-04-07 上午11:03)
 * Description: 设备APP信息
 */
@Service
public class DeviceInfoService extends BaseService<DeviceInfo> {

	/**
	 * 获取设备信息实体
	 * @param deviceInfo
	 * @return
	 */
	public DeviceInfo getActiveDevice(DeviceInfo deviceInfo){
		return super.selectOne(sqlId("getActiveDevice"), deviceInfo);
	}

	/**
	 * 更新设备信息
	 * @param deviceInfo
	 * @return
	 */
	public boolean updateBean(DeviceInfo deviceInfo){
		return super.update(sqlId("update"), deviceInfo);
	}

	/**
	 * 插入设备信息
	 * @param deviceInfo
	 * @return
	 */
	public int insertBean(DeviceInfo deviceInfo){
		if(super.insertAndReturnId(sqlId("insertAndReturnId"), deviceInfo) > 0){
			return deviceInfo.getId();
		}else {
			return 0;
		}
	}

	/**
	 * 获取设备分页信息
	 * @param deviceInfoPagination
	 * @return
	 */
	public Pagination<DeviceInfo> getDeviceInfoPagination(Pagination<DeviceInfo> deviceInfoPagination) {
		return super.getPagination(sqlId("queryDeviceInfoPagination"), sqlId("queryDeviceInfoCount"), deviceInfoPagination);
	}

	/**
	 * 注销 设备注册记录
	 * @param id pns 服务器生成的设备主键id
	 * @param status 1 正常，2 设备注销，3，leanCloud 注销
	 * @return
	 */
	public boolean logout(int id,int status){
		Map<String,Object> map = new HashMap<>();
		map.put("id",id);
		map.put("status",status);
		return super.update(sqlId("logout"), map);
	}

	/**
	 * 根据注册的设备主键id 获取设备信息
	 * @param deviceId
	 * @return
	 */
	public DeviceInfo getActiveDevice(int deviceId) {
		return super.selectOne(sqlId("queryBeanById"), deviceId);
	}

	/**
	 * 检查接受方id
	 * @param appId
	 * @param recipients
	 * @return
	 */
	public List<String> getRecipientsByUser(int appId,String recipients) {
		Map<String,Object> map = new HashMap<>();
		map.put("appId",appId);
		map.put("userCodes", recipients.split(","));
		return super.queryDao.getSqlSession().selectList("DeviceInfo.queryRecipients", map);
	}

	public void updateObjectId(int id, String objectId) {
		Map<String,Object> map = new HashMap<>();
		map.put("id",id);
		map.put("objectId", objectId.trim());
		super.update("updateObjectId",map);
	}

	public List<String> getRecipientsByAppId(int appId) {
		return super.queryDao.getSqlSession().selectList("DeviceInfo.queryRecipientsByAppId", appId);
	}

	/**
	 * 获取注销失败的设备信息集合
	 * @return
	 */
	public List<DeviceInfo> getLogoffFail() {
		return super.selectList(sqlId("queryLogoffFail"));
	}

	/**
	 * 获取注册失败的设备信息集合
	 * @return
	 */
	public List<DeviceInfo> getRegistersFail() {
		return super.selectList(sqlId("queryRegistersFail"));
	}
}
